
package com.scalar.chiptrack.tags;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.utils.ConfigManager;


/**
 * Checks if the user is logged in or not and 
 * redirects to index page if the user is not logged in
 * 
 * This tag should be inserted in all jsp pages.
 * 
 */
public final class CheckLogonTag extends TagSupport {


    /**
     * The page to which we should forward for the user to log on.
     */
    private String m_page = ConfigManager.getDefaultLoginPage();
    
    /**
     * The key against which the user object is stored in the session
     */
    private String m_userKey = SessionKeys.USER_KEY;
    
    /**
     * This is the page to which LoginAction should redirect the user after 
     * successful login.
     */
    private String m_sourcePage = null;
    
    
    public String getPage() {
	return (m_page);
    }
    public void setPage (String page) {
	m_page = page;
    }
    
    public String getUserKey() {
        return m_userKey;
    }
    public void setUserKey (String userKey) {
        m_userKey = userKey;
    }

    public String getSourcePage() {
        return m_sourcePage;
    }
    public void setSourcePage (String srcPage) {
        m_sourcePage = srcPage;
    }

    public int doStartTag() throws JspException {
	return (SKIP_BODY);
    }


    /**
     * redirects to login page.. if 
     *      - the session is invalid or
     *      - the user object is not found in session
     */
    public int doEndTag() throws JspException {

	HttpSession session = pageContext.getSession();
        //System.out.println ("session = " + session);
        
	if ((session != null) && (session.getAttribute (m_userKey) != null)) {
            return EVAL_PAGE;
        }
        else {
            try {
                // Add query string to the sourcePage
                HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
                String queryString = request.getQueryString();
                if (queryString != null) {
                    if (m_sourcePage.indexOf('?') == -1) {
                        m_sourcePage += "?" + queryString;
                    }
                    else {
                        m_sourcePage += "&" + queryString;
                    }
                }

                System.out.println ("Source page = " + m_sourcePage);
                pageContext.getSession().setAttribute (SessionKeys.SOURCE_PAGE_KEY, m_sourcePage);  // Used in LoginAction
		pageContext.forward (m_page);
	    } 
            catch (Exception e) {
		throw new JspException(e.toString());
	    }
            return (SKIP_PAGE);
        }
    }


    public void release() {
        super.release();
        m_page = ConfigManager.getDefaultLoginPage();
    }
}